package com.xci.platform.auth.controller;

import com.xci.platform.auth.entity.FeedbackEntity;
import com.xci.platform.auth.entity.ReplyEntity;
import com.xci.platform.auth.service.FeedbackService;
import com.xci.platform.auth.service.ReplyService;
import com.xci.platform.annotation.Authorize;
import com.xci.platform.core.BoolMessage;
import com.xci.platform.core.JsonGrid;
import com.xci.platform.helper.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 系统反馈控制器
 */
@Authorize
@Slf4j
@Controller
@RequestMapping("/auth/feedback")
public class FeedbackController extends AuthBaseController {
    /**
     * 系统反馈服务
     */
    @Resource
    protected FeedbackService feedbackService;

    /**
     * 系统反馈回复服务
     */
    @Resource
    protected ReplyService replyService;

    //region 页面视图

    /**
     * 系统反馈首页
     */
    @GetMapping
    @Authorize(code = "SystemFeedback")
    public String index() {
        return "auth/feedback/index";
    }

    /**
     * 系统反馈新建页面
     */
    @GetMapping("/feedback")
    public String feedback(ModelMap map) {
        FeedbackEntity entity = new FeedbackEntity();
        entity.setId(StringHelper.guid());
        map.put("entity", entity);
        map.put("authUser", currentUser());
        return "auth/feedback/feedback";
    }

    /**
     * 系统反馈详细页面
     */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        FeedbackEntity entity = feedbackService.queryById(id);
        if (entity == null) return notFound();
        map.put("entity", entity);
        map.put("authUser", currentUser());
        List<ReplyEntity> replyList = replyService.queryByFeedbackId(id);
        map.put("replyList", replyList);
        return "auth/feedback/details";
    }

    //endregion

    //region 数据处理

    /**
     * 系统反馈表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    @Authorize(code = "SystemFeedback.Search")
    public JsonGrid grid(@RequestParam Map params) {
        return BoolMessage.grid(feedbackService.queryPage(params));
    }

    /**
     * 保存系统反馈
     */
    @ResponseBody
    @PostMapping("/save")
    public BoolMessage save(@ModelAttribute FeedbackEntity entity) {
        entity.setStatus(0);
        entity.setReplyCount(0L);
        entity.setReplyStatus(0);
        return feedbackService.save(entity);
    }

    /**
     * 保存系统反馈回复
     */
    @ResponseBody
    @PostMapping("/replysave")
    @Authorize(code = "SystemFeedback.Reply")
    public BoolMessage replySave(String feedbackId, String replyContent) {
        ReplyEntity entity = new ReplyEntity();
        entity.setId(StringHelper.guid());
        entity.setReplyContent(replyContent);
        entity.setFeedbackId(feedbackId);
        return replyService.save(entity);
    }


    /**
     * 关闭系统反馈
     */
    @ResponseBody
    @PostMapping("/close")
    @Authorize(code = "SystemFeedback.Close")
    public BoolMessage close(String id) {
        return feedbackService.close(id);
    }

    //endregion
}
